package ch02.impl;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * @auth 邝志豪
 * @date 2024/1/4 19:36
 */
public class Ex6Impl {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db=dbf.newDocumentBuilder();
        //解析XML文档，得到一个Document对象，即DOM树
        Document doc=db.parse("test/ch02/exXml/ex1.xml");
        NodeList list=doc.getElementsByTagName("Brand");
        for (int i=0; i<list.getLength(); i++){
            Element brandElement=(Element) list.item(i);
            String brandName=brandElement.getAttribute("name");
            if (brandName.equals("SAMSUNG")){
                brandElement.getAttributeNode("SAMSUNG").removeChild(brandElement);
            }
        }
    }
}
